<template>
    <share-json-form 
        :setterFormValues="buildSetterForm" 
        :initValues="{ dataNo: 123 }" 
        :evalUtilMap="{
            _fetchAlarmSource: fetchAlarmSource,
            _fetchAlarmItem: fetchAlarmItem,
            _searchFormSubmit: searchFormSubmit,
        }"
        :inlineLabelWidth="'80px'"
        :components="components"
        />
</template>

<script>
import { ShareJsonForm,ShareInput } from "../../packages"
import { buildSetterForm } from "./buildSetterForm"

export default {
    name: "JsonForm",
    components: {
        ShareJsonForm
    },
    data(){
        return {
            components:[
                ShareInput
            ]
        }
    },
    computed: {
        buildSetterForm
    },
    methods: {
        // 接口-获取报警来源
        fetchAlarmSource() {
            return new Promise((resolve) => {
                const list = [
                    { label: "部标终端", value: '1' },
                    { label: "主动安全设备", value: '2' },
                    { label: "聚集数据", value: '3' },
                ]
                
                setTimeout(() => {
                    resolve(list)
                }, 500);
            })
        },

        // 接口-根据报警来源获取报警项
        fetchAlarmItem({ source }) {
            console.log("source:", source)
            return new Promise((resolve) => {
                const list = [
                    {
                        source: '1',
                        children: [
                            { label: "左顾右盼", value: 11 },
                            { label: "疲劳驾驶", value: 1 },
                            { label: "打哈欠", value: 4 },
                            { label: "车距过近", value: 5 },
                        ]
                    },
                    {
                        source: '2',
                        children: [
                            { label: "车道偏离", value: 9 },
                            { label: "分神驾驶", value: 10 },
                            { label: "双手脱离方向盘", value: 13 },
                        ]
                    },
                    {
                        source: '3',
                        children: [
                            { label: "行驶时玩手机", value: 14 },
                            { label: "接打电话", value: 15 },
                            { label: "前向碰撞", value: 16 }
                        ]
                    },
                ]

                setTimeout(() => {
                    resolve(list.find(v => v.source == source?.value)?.children)
                }, 500);
            })
        },

        // 提交表单-获取表单values
        searchFormSubmit(values) {
            this.searchFormValues = values;
            console.log("jsonForm's values:", values)
        },
    }
}
</script>

<style></style>